package com.ambow.store.service.impl;

import com.ambow.store.entity.Product;
import com.ambow.store.mapper.ProductMapper;
import com.ambow.store.service.exception.CartTypeException;
import com.ambow.store.vo.CartVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ambow.store.entity.Cart;
import com.ambow.store.service.CartService;
import com.ambow.store.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
* @author MyComputer
* @description 针对表【t_cart】的数据库操作Service实现
* @createDate 2022-11-12 13:17:15
*/
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService{

    //因为需要使用到商品的价格，所以引入商品持久层
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CartMapper cartMapper;

    @Override
    public void addToCart(Cart cart, HttpSession session) {

        LambdaQueryWrapper<CartVO> cartLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUid,session.getAttribute("uid"));
        cartLambdaQueryWrapper.eq(Cart::getPid,cart.getPid());
        //查询当前数据库中是否有数据
        Cart result = this.getOne(cartLambdaQueryWrapper);
        //查询商品列表中的信息
        Product product = productMapper.selectById(cart.getPid());

        if(result == null){
            cart.setCreatedUser((String) session.getAttribute("username"));
            cart.setCreatedTime(new Date());
            cart.setUid((Integer) session.getAttribute("uid"));
            cart.setModifiedUser((String) session.getAttribute("username"));
            cart.setModifiedTime(new Date());
            cart.setPrice(product.getPrice());
            boolean save = this.save(cart);
            if(!save){
                throw new CartTypeException("添加购物车时发生未知信息错误");
            }
        }else {
            result.setNum(cart.getNum()+ result.getNum());
            boolean b = this.updateById(result);
            if(!b){
                throw new CartTypeException("添加购物车时发生未知信息错误");
            }
        }
    }

    @Override
    public List<CartVO> findVOByUid(Integer uid) {
        return cartMapper.findVOByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid, String username) {
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getCid,cid);
        Cart result = this.getOne(cartLambdaQueryWrapper);
        if (result == null){
            throw new CartTypeException("数据不存在");
        }

        int num = result.getNum() + 1;

        result.setNum(num);
        result.setModifiedTime(new Date());
        result.setModifiedUser(username);

        boolean b = this.updateById(result);

        if( !b ) {
            throw new CartTypeException("数据更新失败");
        }

        return num;
    }

    @Override
    public List<CartVO> getVOByCid(Integer uid, Integer[] cids) {
        List<CartVO> voByCid = cartMapper.findVOByCid(cids);
        Iterator<CartVO> iterator = voByCid.iterator();
        while (iterator.hasNext()){
            CartVO next = iterator.next();
            if( !next.getUid().equals(uid) ){//表示当前商品不属于当前用户
                //从集合中移除
                iterator.remove();
            }
        }
        return voByCid;
    }
}




